
var recordId = getQueryString("recordId");


var getPeas = {
    init: function () {
        this.getData();
    },
    eventLisenter: function () {
        $(".intenthtml").click(function(){
            window.location.href=$(this).attr("href");
        });

        $(".immediately").click(function(){
            getPeas.recieveG();
        });
        // $(".goShopping").click(function(){
        //
        // });
        $(".roger").click(function(){
            $(".popups").css("display", "none");
            $(".succeed").css("display", "none");
            $(".defeated").css("display", "none");
        });
    },
    recieveG:function(){
        new AjaxRequest({
            type: "get",
            url: apiService.recieveGuobi+"?recordId="+recordId,
            param:"",
            callBack: function (res) {
                if(res.code==302){
                    window.location.replace(res.url);
                }else if(res.code=="0001"){
                    alert(res.message);
                }else if(res.success){
                    $(".goToShopping").attr("href",res.url1);
                    $(".goToLook").attr("href",res.url2);
                    $(".popups").css("display", "flex");
                    $(".succeed").css("display", "block");
                }else{
                    $(".goTo").attr("href",res.url1);
                    $(".popups").css("display", "flex");
                    $(".defeated").css("display", "block");
                }
            },
            failBack: function () {
            }
        });
    },
    sendsms:function(phone){
        new AjaxRequest({
            type: "get",
            url: apiService.sendSms+"?recordId="+recordId+"&mobile="+phone,
            param:'',
            callBack: function (res) {
            },
            failBack: function () {
            }
        });
    },
    getData: function () {
        alert(recordId);
        new AjaxRequest({
            type: "get",
            url: apiService.recieveGuobiInit+"?recordId="+recordId,
            param:'',
            callBack: function (res) {
                if(res.code==302){
                    window.location.href= res.url;
                    return;
                }
                $(".senderNickname").text(res.payload.senderNickname);
                $(".hint").text(res.payload.guobiValueDesc);
                $(".num").text(res.payload.sendCnt);
                settime(res.payload.expireSeconds);
                var code = getQueryString("code");
                if(!code){
                    return
                }
                if(code==1){
                    $(".popups").css("display", "flex");
                    $(".succeed").css("display", "block");
                }else if(code==2){
                    var url=getQueryString("url1");
                    // decodeURIComponent(url);
                    $(".goTo").attr("href",decodeURIComponent(url));
                    $(".popups").css("display", "flex");
                    $(".defeated").css("display", "block");
                }
            },
            failBack: function () {

            }
        });
    }
}
//正则获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
function isEmpty(str) {
    return (!str || 0 === str.length);
}
function settime(countdown) {
    if (countdown == 0) {
        $(".immediately").addClass("disable");
        $(".immediately").text("已超时");
        // countdown = 60;
        return;
    } else {
        countdown--;
        $(".otime").text(timeformat(countdown));
        setTimeout(function() {
            settime(countdown);
        },1000)
    }
}
function timeformat(s){
    var t;
    if(s > -1){
        var hour = Math.floor(s/3600);
        var min = Math.floor(s/60) % 60;
        var sec = s % 60;
        if(hour < 10) {
            t = '0'+ hour + ":";
        } else {
            t = hour + ":";
        }
        if(min < 10){t += "0";}
        t += min + ":";
        if(sec < 10){t += "0";}
        t += sec;
    }
    return t;
}
